<template>
  <div class="dialogCom">
    <el-dialog title="编辑员工" width="50%" :visible.sync="isShow">
      <el-form ref="form" label-width="100px" :model="form" :rules="rules">
        <el-form-item prop="username" label="姓名：">
          <el-input v-model.trim="form.username" />
        </el-form-item>
        <el-form-item prop="mobile" label="手机号：">
          <el-input v-model.trim="form.mobile" />
        </el-form-item>
        <el-form-item prop="timeOfEntry" label="入职时间：">
          <el-date-picker
            v-model.trim="form.timeOfEntry"
            type="date"
            placeholder="选择日期"
          />
        </el-form-item>
        <el-form-item prop="formOfEmployment" label="聘用形式：">
          <el-select v-model.trim="form.formOfEmployment">
            <el-option
              v-for="item in hireType"
              :key="item.id"
              :label="item.value"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item prop="workNumber" label="工号：">
          <el-input v-model.trim="form.workNumber" />
        </el-form-item>
        <el-form-item prop="departmentName" label="部门：">
          <div ref="treeEvent">
            <el-input
              v-model.trim="form.departmentName"
              readonly
              @focus="focusEvent"
            />
            <el-tree
              v-show="treeShow"
              :data="list"
              :props="{ label: 'name' }"
              @node-click="treeClick"
            />
          </div>
        </el-form-item>
        <el-form-item prop="correctionTime" label="转正时间：">
          <el-date-picker
            v-model.trim="form.correctionTime"
            type="date"
            placeholder="选择日期"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="btn">
          <el-button type="primary" @click="submit">提交</el-button>
          <el-button>取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script>
import employees from '@/assets/constant/employees'
import { companyDepartmentGet } from '@/api/departments'
import { sysUserPost } from '@/api/employees.js'
export default {
  name: 'DialogCom',
  data() {
    return {
      hireType: employees.hireType,
      isShow: false,
      treeShow: false,
      list: [],
      form: {
        username: '', //	string	非必须		姓名
        mobile: '', //	string	非必须		手机号
        timeOfEntry: '', //	string	非必须		入职时间
        formOfEmployment: '', //	number	非必须		聘用形式
        workNumber: '', //	string	非必须		工号
        departmentName: '', //	string	非必须		组织名称
        correctionTime: '' //	string	非必须		转正时间
      },
      rules: {
        username: [
          { required: true, message: '必填', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              const req = /[\u4e00-\u9fa5]{2,13}/gm
              req.test(value)
                ? callback()
                : callback(new Error('请输入正确的姓名'))
            },
            trigger: 'blur'
          }
        ],
        mobile: [
          { required: true, message: '必填', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              const req =
                /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              req.test(value)
                ? callback()
                : callback(new Error('请输入正确的手机号'))
            },
            trigger: 'blur'
          }
        ],
        timeOfEntry: [{ required: true, message: '必填', trigger: 'change' }],
        formOfEmployment: [
          { required: true, message: '必填', trigger: 'blur' }
        ],
        workNumber: [{ required: true, message: '必填', trigger: 'blur' }],
        departmentName: [
          { required: true, message: '必填', trigger: 'change' }
        ],
        correctionTime: [{ required: true, message: '必填', trigger: 'change' }]
      }
    }
  },
  mounted() {
    document.addEventListener('click', this.docClick)
  },
  beforeDestroy() {
    document.removeEventListener('click', this.docClick)
  },
  methods: {
    async focusEvent() {
      if (this.list.length === 0) {
        const res = await companyDepartmentGet()
        this.list = this.IntegrateData(res.data.depts)
      }
      this.treeShow = true
    },
    IntegrateData(arr, str = '') {
      return arr.filter((item) => {
        if (item.pid === str) {
          item.children = this.IntegrateData(arr, item.id)
          return item
        }
      })
    },
    treeClick(item) {
      this.form.departmentName = item.name
    },
    async submit() {
      await sysUserPost(this.form)
      this.$message.success('新增成功')
      this.$emit('handleCurrentChange', 1)
    },
    docClick(e) {
      if (this.isShow && !this.$refs.treeEvent.contains(e.target)) {
        this.treeShow = false
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.dialogCom {
  .btn {
    text-align: center;
  }
  .el-form-item {
    width: 400px;
  }
}
</style>
